<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <home-swiper :banners="banners"></home-swiper>
    <recommend-view :recommends="recommends"></recommend-view>
    <FeatureView></FeatureView>
    <tab-control class="tab-control" :titles="titles" @tabClick="tabClick"></tab-control>
    <goods-list :goods="showGoods"></goods-list>

    <ul>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
      <li>1213</li>
    </ul>
  </div>
</template>

<script>
  import NavBar from "../../components/common/navbar/NavBar";
  import HomeSwiper from './childComps/HomeSwiper';
  import {getHomeMultidata, getHomeGoods} from "../../network/home";
  import RecommendView from "./childComps/RecommendView";
  import FeatureView from "./childComps/FeatureView";
  import TabControl from "../../components/content/tabControl/TabControl";
  import GoodsList from "../../components/content/goods/GoodsList";

  export default {
    name: "Home",
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList
    },
    data() {
      return {
        result: null,
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []}
        },
        titles: ["流行", "新款", "精选"],
        currentType: 'pop'
      }
    },
    methods: {
      // 事件监听
      tabClick(index) {
        console.log(index)
        switch (index) {
          case 0:
            this.currentType = 'pop';
            break;
          case 1:
            this.currentType = 'new';
            break;
          case 2:
            this.currentType = 'sell';
            break;
        }
      },
      // 网络请求
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoods(type) {
        const page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          console.log(res)
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1
        })
      }
    },
    created() {
      // 请求多个数据
      this.getHomeMultidata();
      // 请求商品数据
      this.getHomeGoods('pop');
      this.getHomeGoods('new');
      this.getHomeGoods('sell');

    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list;
      }
    }
  }
</script>

<style scoped>
  .home-nav {
    background-color: var(--color-tint);
    color: white;

    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 5555;
  }

  #home {
    margin-top: 44px;
  }

  .tab-control {
    position: sticky;
    top: 44px;
    background-color: white;
    z-index: 555;
  }
</style>
